package com.zeroz.domain.service;

import com.alipay.api.AlipayApiException;
import com.zeroz.domain.order.adapter.port.IProductPort;
import com.zeroz.domain.order.adapter.repository.IOrderRepository;
import com.zeroz.domain.order.model.aggregate.CreateOrderAggregate;
import com.zeroz.domain.order.model.entity.OrderEntity;
import com.zeroz.domain.order.model.entity.PayOrderEntity;
import com.zeroz.domain.order.model.entity.ProductEntity;
import com.zeroz.domain.order.model.entity.ShopCartEntity;
import com.zeroz.domain.order.model.valobj.OrderStatusVO;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;

@Slf4j
public abstract class AbstractOrderService implements IOrderService{

    protected final IOrderRepository orderRepository;
    protected final IProductPort productPort;

    protected AbstractOrderService(IOrderRepository orderRepository, IProductPort productPort) {
        this.orderRepository = orderRepository;
        this.productPort = productPort;
    }


    @Override
    public PayOrderEntity createOrder(ShopCartEntity shopCartEntity) throws Exception {
        //查询当前用户是否存在调单或者或未支付的订单
        OrderEntity unpaidOrderEntity = orderRepository.queryUnPayOrder(shopCartEntity);
        if (null != unpaidOrderEntity
                && OrderStatusVO.PAY_WAIT.equals(unpaidOrderEntity.getOrderStatusVO())) {
            log.info("创建订单-存在，已存在未支付订单。userId:{} productId:{} orderId:{}"
                    , shopCartEntity.getUserId(), shopCartEntity.getProductId(), unpaidOrderEntity.getOrderId());
            return PayOrderEntity.builder()
                    .orderId(unpaidOrderEntity.getOrderId())
                    .payUrl(unpaidOrderEntity.getPayUrl())
                    .build();
        } else if(null != unpaidOrderEntity
                && OrderStatusVO.CREATE.equals(unpaidOrderEntity.getOrderStatusVO())) {
            log.info("创建订单-存在，存在未创建支付单订单，创建支付单开始 userId:{} productId:{} orderId:{}", shopCartEntity.getUserId(), shopCartEntity.getProductId(), unpaidOrderEntity.getOrderId());
            PayOrderEntity payOrderEntity = doPrepayOrder(shopCartEntity.getUserId(),shopCartEntity.getProductId()
                    ,unpaidOrderEntity.getProductName(),unpaidOrderEntity.getOrderId(),unpaidOrderEntity.getTotalAmount());
            return PayOrderEntity.builder()
                    .orderId(payOrderEntity.getOrderId())
                    .payUrl(payOrderEntity.getPayUrl())
                    .build();
        }
        ProductEntity productEntity
                = productPort.queryProductByProductId(shopCartEntity.getProductId());
        OrderEntity orderEntity = CreateOrderAggregate.buildOrderEntity(productEntity.getProductId(), productEntity.getProductName());
        CreateOrderAggregate createOrderAggregate = CreateOrderAggregate.builder()
                .userId(shopCartEntity.getUserId())
                .productEntity(productEntity)
                .orderEntity(orderEntity)
                .build();
        this.doSaveOrder(createOrderAggregate);
        PayOrderEntity payOrderEntity = doPrepayOrder(shopCartEntity.getUserId(),shopCartEntity.getProductId()
                ,productEntity.getProductName(),orderEntity.getOrderId(),productEntity.getPrice());
        log.info("创建订单-完成，生成支付单。userId: {} orderId: {} payUrl: {}", shopCartEntity.getUserId(), orderEntity.getOrderId(), payOrderEntity.getPayUrl());
        return PayOrderEntity.builder()
                .orderId(orderEntity.getOrderId())
                 .payUrl(payOrderEntity.getPayUrl())
                .build();
    }

    protected abstract PayOrderEntity doPrepayOrder(String userId, String productId, String productName,
                                                    String orderId, BigDecimal totalAmount) throws AlipayApiException;

    protected abstract void doSaveOrder(CreateOrderAggregate createOrderAggregate);

}
